/**
 * Created by isoft-work 5 on 11/16/14.
 */

//<div class="pam">Tài khoản của bạn đã cập nhật thành công</div>

$(document).ready(function(){
//   render
   renderEditAccount();
//   event
   bindEventEditAccount();
});

function bindEventEditAccount() {
   ISS.EVENT.bindEvent('#edit_accoutn_send_info', 'click', function () {
      clearValidate();
      if(validateEditAccount()) {
         var userInfo = {};
         mapViewToUser(userInfo);
         updateUser(userInfo);
      }
      else {
         var htmlText = '<div id = "message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Tài khoản của bạn cập nhật không thành công</div>\
                            </div>';
         ISS.DOM.before('.l-hasSidebar', htmlText);
      }
   });
}

function validateEditAccount() {
   var firstName, lastName, result;
   firstName = ISS.DOM.getVal('#EditForm_first_name');
   lastName = ISS.DOM.getVal('#EditForm_last_name');

   if(!firstName) {
      ISS.DOM.addClass('#EditForm_first_name', 'error');
      result = false;
   }

   if(!lastName) {
      ISS.DOM.addClass('#EditForm_last_name', 'error');
      result = false;
   }

   if(firstName && lastName) {
      result = true;
   }
   return result;
}

function clearValidate() {
   ISS.DOM.remove('#message-error');
   ISS.DOM.removeClass('#EditForm_first_name', 'error');
   ISS.DOM.removeClass('#EditForm_last_name', 'error');
}

function updateUser(userInfo) {
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/EditAccount/updateInfoUser/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(userInfo),
      success: function (data) {
         var responseInfo = data.responseObject;
         updateUserComplete(responseInfo);
      }
   });
}

function updateUserComplete(responseInfo) {
   if(responseInfo) {
     ISS.WINDOW.location('/jsp/user/user-account.jsp');
   }
   else {
      ISS.DOM.remove('#message-error');
      var htmlText = '<div id="message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Tài khoản của bạn cập nhật không thành công</div>\
                            </div>';
      ISS.DOM.before('.l-hasSidebar', htmlText);
   }
}

function mapViewToUser(userInfo) {
   var day, month, year;
   day = ISS.DOM.getVal('#EditForm_day');
   month = ISS.DOM.getVal('#EditForm_month');
   year = ISS.DOM.getVal('#EditForm_year');
   var dateString = year + '/' + month + '/' + day + '/';
   userInfo.maTV = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName'): ISS.COOKIE.getCookie('userName');
   userInfo.ten = ISS.DOM.getVal('#EditForm_first_name');
   userInfo.ho = ISS.DOM.getVal('#EditForm_last_name');
   userInfo.soDT = ISS.DOM.getVal('#EditForm_phone');
   userInfo.diaChi = ISS.DOM.getVal('#EditForm_address');
   userInfo.gioiTinh = ISS.DOM.getVal('#EditForm_gender');
   userInfo.ngaySinh = new Date(dateString);
}

function renderEditAccount() {
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   if(!useName) {
      ISS.WINDOW.location('/jsp/user/user-login.jsp');
   }
   else {
      getInfoUser(useName);
   }
}

function getInfoUser(useName) {
   var user = {};
   user.maTV = useName;
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/EditAccount/getInfoUser/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(user),
      success: function (data) {
         var responseInfo = data.responseObject;
         getInfoUserComplete(responseInfo);
      }
   });
}

function getInfoUserComplete(userInfo) {
   ISS.DOM.text('#EditForm_email', userInfo.maTV);
   ISS.DOM.setVal('#EditForm_first_name',userInfo.ten);
   ISS.DOM.setVal('#EditForm_last_name',userInfo.ho);
   ISS.DOM.setVal('#EditForm_phone',userInfo.soDT);
   ISS.DOM.setVal('#EditForm_address',userInfo.diaChi);
   ISS.DOM.setVal('#EditForm_gender',userInfo.gioiTinh);
   if(userInfo.gioiTinh == 'NAM') {
      ISS.DOM.setAtt('#ISS.DOM.setVal_nam', 'selected');
   }
   else {
      ISS.DOM.setAtt('#ISS.DOM.setVal_nu', 'selected');
   }
   if(!userInfo.ngaySinh){
      ISS.DOM.setVal('#EditForm_day', '');
      ISS.DOM.setVal('#EditForm_month', '');
      ISS.DOM.setVal('#EditForm_year', '');
   }
   else {
      var _ngaySinh = new Date(userInfo.ngaySinh);
      ISS.DOM.setVal('#EditForm_day', _ngaySinh.getDate());
      ISS.DOM.setVal('#EditForm_month', ISS.DATE.getMonthString(_ngaySinh));
      ISS.DOM.setVal('#EditForm_year', _ngaySinh.getFullYear());
   }
}